/*
*                             _ooOoo_
*                            o8888888o
*                            88" . "88
*                            (| -_- |)
*                            O\  =  /O
    *                         ____/`-&#45;&#45;'\____
*                       .'  \\|     |//  `.
    *                      /  \\|||  :  |||//  \
*                     /  _||||| -:- |||||-  \
*                     |   | \\\  -  /// |   |
*                     | \_|  ''\-&#45;&#45;/''  |   |
*                     \  .-\__  `-`  ___/-. /
*                   ___`. .'  /&#45;&#45;.&#45;&#45;\  `. . __
*                ."" '<  `.___\_<|>_/___.'  >'"".
*               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
*               \  \ `-.   \_ __\ /__ _/   .-` /  /
*          ======`-.____`-.___\_____/___.-`____.-'======
*                             `=-&#45;&#45;='
*          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*                     佛祖保佑        永无BUG
*            佛曰:
*                   写字楼里写字间，写字间里程序员；
*                   程序人员写程序，又拿程序换酒钱。
*                   酒醒只在网上坐，酒醉还来网下眠；
*                   酒醉酒醒日复日，网上网下年复年。
*                   但愿老死电脑间，不愿鞠躬老板前；
*                   奔驰宝马贵者趣，公交自行程序员。
*                   别人笑我忒疯癫，我笑自己命太贱；
*                   不见满街漂亮妹，哪个归得程序员？
*/
import {axiosGetUserInfo, axiosLogin, axiosRegister, axiosSetUserInfo, axiosText} from "../../api";
import {getUserInfo, getUserState, setUserInfo, setUserState} from "../../storage/storage";

const actions = {
    async getUser(context) {
        let result = await axiosText();
        if (result.status === 200) {
            context.commit("GET_USER", result.data)
        }
    },
    async userRegister(context, value) {
        await axiosRegister({...value}).then(async result => {
            if (result.status === 200) {
                if (result.data === "Normal") {
                    await context.dispatch("getUserInfo", {...value});
                    context.commit("LOGIN_SUCCESS", {...value, loginState: true})
                } else if (result.data === "RepeatError") {
                    context.commit("FAIL", "账号已存在，请换一个")
                } else if (result.data === "DataFormatError") {
                    context.commit("FAIL", "账号、密码或者昵称格式错误")
                } else if (result.data === "EmptyError") {
                    context.commit("FAIL", "账号、密码或者昵称为空");
                } else if (result.data === "DatabaseError") {
                    context.commit("FAIL", "数据库错误");
                } else {
                    context.commit("FAIL", "服务器错误，请联系管理员");
                }
            }
        })
    },
    async userLogin(context, value) {
        await axiosLogin({...value}).then(async result => {
            if (result.status === 200) {
                if (result.data === "Normal") {
                    await context.dispatch("getUserInfo", {account: value.account});
                    context.commit("LOGIN_SUCCESS", {
                        ...value,
                        username: context.state.userInfo[0].username,
                        loginState: true
                    })
                } else if (result.data === "DataError") {
                    context.commit("FAIL", "账号或者密码错误")
                } else if (result.data === "DataFormatError") {
                    context.commit("FAIL", "账号或者密码格式错误")
                } else if (result.data === "EmptyError") {
                    context.commit("FAIL", "账号、密码为空");
                } else if (result.data === "DatabaseError") {
                    context.commit("FAIL", "数据库错误");
                } else if (result.data === "DataNotFoundError") {
                    context.commit("FAIL", "账号不存在，请验证");
                } else {
                    context.commit("FAIL", "服务器错误，请联系管理员");
                }
            }
        })

    },
    async getUserInfo(context, value) {
        await axiosGetUserInfo({account: value.account}).then(async result => {
            if (result.status === 200) {
                await context.commit("SET_USER_INFO", result.data);
            }
        });
    },
    async setUserInfo(context, value) {
        await axiosSetUserInfo({...value}).then(async result => {
            if (result.status === 200) {
                if (result.data === "DataFormatError") {
                    context.commit("FAIL", "数据格式错误")
                } else if (result.data === "EmptyError") {
                    context.commit("FAIL", "数据为空");
                } else if (result.data === "DataNotFoundError") {
                    context.commit("FAIL", "未知账号");
                } else if (result.data === "DatabaseError") {
                    context.commit("FAIL", "数据库错误");
                } else if (result.data === "Normal") {
                    context.commit("FAIL", "修改成功");
                    context.commit("SET_USER_INFO", {...value});
                } else {
                    context.commit("FAIL", "服务器错误");
                }
            }
        })
    }
};
const mutations = {
    GET_USER(state, value) {
        state.userList = value;
    },
    LOGIN_SUCCESS(state, value) {
        state.userState = value;
        setUserState(value);
    },
    FAIL(state, value) {
        state.failCode = value;
    },
    SET_USER_INFO(state, value) {
        state.userInfo = value;
        setUserInfo(value);
    }
};
const state = {
    userList: [],
    userState: getUserState() || {loginState: false},
    failCode: "",
    userInfo: getUserInfo(),
};

export default {
    actions, mutations, state
}
